/**
 * $Revision: 1.2 $
 * $Date: 2007/06/09 12:13:06 $
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package com.regal.conferencepush;

import javax.swing.SwingUtilities;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;

import com.regal.packet.ConferencePush;
import com.regal.packet.ConferencePushProvider;

/**
 *
 */
public class ConferencePushPlugin implements Plugin {


    public void initialize() {
    	System.out.println("conferencePushPlugin added");
        ProviderManager.getInstance().addExtensionProvider("query", "jabber:message:conference_push", new ConferencePushProvider());
        Message message = new Message();
        message.addExtension(new ConferencePush("",""));
        SparkManager.getConnection().addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
            	System.out.println("Conference Push processPacket start");
                if (packet instanceof Message) {
                    final Message message = (Message)packet;
                    if (message.getExtension("query", "jabber:message:conference_push") != null) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                            	System.out.println("about to show conference");
                                showConference(message);
                            }
                        });
                    }
                }
            }
        }, new PacketTypeFilter(Message.class));

     
    }

    private void showConference(Message message) {
    	ConferencePush conferencePacket = (ConferencePush)message.getExtension("query", "jabber:message:conference_push");
    	//SparkManager.getWorkspace().getStatusBar().setConference(conferencePacket.getNewsItems());
    	System.out.println("jid is "+conferencePacket.getJid());
    	SparkManager.getWorkspace().getStatusBar().setConference(conferencePacket.getTitle(),conferencePacket.getJid());
    }

    public void shutdown() {
    }

    public boolean canShutDown() {
        return true;
    }

    public void uninstall() {
    }
}
